<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_order_goods}}".
 *
 * @property int $id
 * @property int $order_id 用户ID
 * @property int $product_id 产品ID
 * @property string $name 产品名称
 * @property string $goods_name 商品名称
 * @property int $goods_id 商品ID
 * @property string $goods_cover 商品封面
 * @property string $goods_code 商品编码
 * @property string $goods_price 商品价格（元）
 * @property string $goods_discount 商品优惠金额（元）
 * @property int $goods_number 商品数量
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class ShopOrderGoods extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_order_goods}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['order_id', 'product_id', 'goods_id', 'goods_number', 'created_at', 'updated_at'], 'integer'],
            [['goods_price', 'goods_discount'], 'number'],
            [['name', 'goods_name'], 'string', 'max' => 50],
            [['goods_cover'], 'string', 'max' => 255],
            [['goods_code'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'order_id' => '用户ID',
            'product_id' => '产品ID',
            'name' => '产品名称',
            'goods_name' => '商品名称',
            'goods_id' => '商品ID',
            'goods_cover' => '商品封面',
            'goods_code' => '商品编码',
            'goods_price' => '商品价格（元）',
            'goods_discount' => '商品优惠金额（元）',
            'goods_number' => '商品数量',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getThumb() {
        return Url::attach($this->goods_cover);
    }

    public function getAmount() {
        return $this->goods_price * $this->goods_number - $this->goods_discount;
    }

    public function fields() {
        return [
            'id' => 'goods_id',
            'name',
            'thumb',
            'sku' => 'goods_name',
            'price' => 'goods_price',
            'discount' => 'goods_discount',
            'num' => 'goods_number',
            'code' => 'goods_code',
        ];
    }

}
